package handler

import (
	"github.com/gin-gonic/gin"
	"icasset/internal/dao"
	"icasset/internal/model"
	"log"
	"net/http"
	"strconv"
)

type DemandHandler struct {
	// 我们推荐注入 DAO 层，而不是直接注入 *gorm.DB，这更符合分层架构
	demandDAO *dao.GormDemandDAO
}

func NewDemandHandler(demandDAO *dao.GormDemandDAO) *DemandHandler {
	return &DemandHandler{demandDAO: demandDAO}
}
func (h *DemandHandler) AddDemand(c *gin.Context) {
	var demand model.IcDemand
	if err := c.ShouldBindJSON(&demand); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 添加日志调试，查看接收到的数据
	log.Printf("Received demand data: %+v", demand)

	if err := h.demandDAO.AddDemand(c, &demand); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create demand: " + err.Error()})
		return
	}
	c.JSON(http.StatusCreated, demand)
}

func (h *DemandHandler) GetAllDemands(c *gin.Context) {
	demands, err := h.demandDAO.GetAllDemands(c)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get demands: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, demands)
}

func (h *DemandHandler) GetDemandByPkid(c *gin.Context) {
	pkidStr := c.Param("pkid")
	pkid, err := strconv.ParseInt(pkidStr, 10, 6)
	demand, err := h.demandDAO.GetDemandByPkid(c, pkid)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get demand: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, demand)
}

func (h *DemandHandler) GetDemandByUserId(c *gin.Context) {
	userId := c.Param("userId")
	demands, err := h.demandDAO.GetDemandByUserId(c, userId)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get demands: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, demands)
}

func (h *DemandHandler) GetDemandBySellerId(c *gin.Context) {
	sellerId := c.Param("sellerId")
	demands, err := h.demandDAO.GetDemandBySellerId(c, sellerId)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get demands: " + err.Error()})
		return
	}
	c.JSON(http.StatusOK, demands)
}

//func (h *DemandHandler) QueryDemandPage(c *gin.Context, pageNum, pageSize int) {
//	demands, err := h.demandDAO.QueryDemandPage(c, pageNum, pageSize)
//	if err != nil {
//		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get demands: " + err.Error()})
//		return
//	}
//	c.JSON(http.StatusOK, demands)
//}

func (h *DemandHandler) ConfirmDemandByBuyer(c *gin.Context) {
	//pkidStr := c.Param("pkid")
	//pkid, err := strconv.ParseInt(pkidStr, 10, 6)
	var demand *model.IcDemand
	if err := c.ShouldBindJSON(&demand); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	h.demandDAO.ConfirmDemandByBuyer(c, demand.Pkid, demand.FinalPrice, demand.DeadlineTime)
}

func (h *DemandHandler) CancelDemandByBuyer(c *gin.Context) {
	var demand *model.IcDemand
	if err := c.ShouldBindJSON(&demand); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.demandDAO.CancelDemandByBuyer(c, demand)
}
